package com.android.love.ui;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.android.love.R;
import com.android.love.util.ImageUtils;
import com.android.love.util.LogUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;

public class ScreenSlidePageFragment extends Fragment {
	// Universal Image Loader

	public interface OnImgClickLister {
		void onImgClick();
	}

	public void setOnImgClickLister(OnImgClickLister listener) {
		mOnImgClickLister = listener;
	}

	private OnImgClickLister mOnImgClickLister;// 图片点击监听

	/**
	 * The argument key for the page number this fragment represents.
	 */
	public static final String ARG_PAGE = "page";
	// public static final String ARG_TYPE = "type";

	public static final String ARG_IMAGES = "images";

	/**
	 * The fragment's page number, which is set to the argument value for
	 * {@link #ARG_PAGE}.
	 */
	private int mPageNumber;
	// private List<PicItem> lists;
	private String[] imgs;
	private ViewGroup rootView;

	// private int type;

	/**
	 * Factory method for this fragment class. Constructs a new fragment for the
	 * given page number.
	 */
	public static ScreenSlidePageFragment create(int pageNumber, String[] imgs) {
		ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
		Bundle args = new Bundle();
		args.putInt(ARG_PAGE, pageNumber);
		// args.putInt(ARG_TYPE, type);
		// args.putStringArray(ARG_IMAGES, lists);
		args.putStringArray(ARG_IMAGES, imgs);
		// args.putSerializable(ARG_IMAGES, (Serializable) lists);
		fragment.setArguments(args);
		return fragment;
	}

	public ScreenSlidePageFragment() {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mPageNumber = getArguments().getInt(ARG_PAGE);
		// type = getArguments().getInt(ARG_TYPE);
		// lists = (List<PicItem>) getArguments().getSerializable(ARG_IMAGES);
		imgs = getArguments().getStringArray(ARG_IMAGES);

		// mOptions = new DisplayImageOptions.Builder()
		// .showImageForEmptyUri(R.drawable.ic_empty)
		// .showImageOnFail(R.drawable.ic_error).resetViewBeforeLoading()
		// .cacheOnDisc().imageScaleType(ImageScaleType.EXACTLY)
		// .bitmapConfig(Bitmap.Config.RGB_565)
		// .displayer(new SimpleBitmapDisplayer()).build();

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// if (type == DONG_PIC) {
		// rootView = (ViewGroup) inflater.inflate(
		// R.layout.item_pager_image_gif, container, false);
		// } else {
		rootView = (ViewGroup) inflater.inflate(R.layout.pager_item, container,
				false);
		// }

		// // Set the title view to show the page number.
		// ((TextView) rootView.findViewById(android.R.id.text1))
		// .setText(getString(R.string.title_template_step,
		// mPageNumber + 1));
		displayImageFile(rootView);
		return rootView;
	}

	private void displayImageFile(ViewGroup view) {
		ImageView imageView = (ImageView) view.findViewById(R.id.image);
		final ProgressBar spinner = (ProgressBar) view
				.findViewById(R.id.loading);

		// mImageLoader.displayImage(mImages[mPageNumber], imageView,
		// mOptions);
		spinner.setVisibility(View.GONE);
		ImageLoader.getInstance().displayImageForSize(imgs[mPageNumber],
				imageView, ImageUtils.getOptions(R.drawable.zw4),
				new SimpleImageLoadingListener() {
					@Override
					public void onLoadingStarted(String imageUri, View view) {
						super.onLoadingStarted(imageUri, view);
						spinner.setVisibility(View.VISIBLE);
					}

					@Override
					public void onLoadingComplete(String imageUri, View view,
							Bitmap loadedImage) {
						spinner.setVisibility(View.GONE);
					}

					@Override
					public void onLoadingFailed(final String imageUri,
							View view, FailReason failReason) {
						Toast.makeText(getActivity(), "出了点小差错，请稍后重试",
								Toast.LENGTH_SHORT).show();

						spinner.setVisibility(View.GONE);
					}
				}, 0, 0, 0, 0, 0, 0, 1f, false);
		imageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				LogUtil.i("onClick", "onClick");
				mOnImgClickLister.onImgClick();
			}
		});
	}

	/**
	 * Returns the page number represented by this fragment object.
	 */
	public int getPageNumber() {
		return mPageNumber;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		LogUtil.i("destroy", "destroy");
		// This should be the ID of this fragments (ScreenSlidePageFragment)
		// layout
		unbindDrawables(rootView.findViewById(R.id.content));
	}

	private void unbindDrawables(View view) {
		if (view.getBackground() != null) {
			view.getBackground().setCallback(null);
		}
		if (view instanceof ViewGroup && !(view instanceof AdapterView)) {
			for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
				unbindDrawables(((ViewGroup) view).getChildAt(i));
			}
			((ViewGroup) view).removeAllViews();
		}
	}

}
